
// Copyright (C) 2007 Richard Uhler
// This file is part of Esiply.
//
// Esiply is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, Version 2,
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include "NameValue.h"
#include "NameValueList.h"
#include "Object.h"
#include "ObjectExpression.h"
#include "String.h"
#include "Value.h"

ObjectExpression::ObjectExpression(NameValueList* nvl)
    : m_nvl(nvl)
{}

Value* ObjectExpression::evaluate(Scope* scope)
{
    // Ecma 11.1.5
    // 1. Create a new object as if by the expression new Object();
    Object* obj = new Object();

    for (int i = 0; i < m_nvl->size(); i++)
    {
        NameValue* nv = m_nvl->get(i);

        // 2. Evaluate name
        String* name = nv->get_name();

        // 3. Evaluate exp
        // 4. Call getValue on that
        Value* val = nv->get_value()->evaluate(scope)->getValue();

        // 5. Call the [[put]] method on obj
        obj->put(name, val, PA_EMPTY);
    }

    // 6. Return result
    return obj;
}

